$(function () {

    vm.getFaultTypeTree();

    $("#jqGrid").Grid({
        url: '../faultbase/list',
        colModel: [
            {label: '故障大类', name: 'devTypeNameB', index: 'DEV_TYPE_NAME_B', width: 80},
            {label: '故障中类', name: 'devTypeNameM', index: 'DEV_TYPE_NAME_M', width: 80},
            {label: '故障小类', name: 'devTypeNameS', index: 'DEV_TYPE_NAME_S', width: 80},
            {label: '故障类型', name: 'faultType', index: 'FAULT_TYPE', width: 80},
            {label: '故障代码', name: 'faultCode', index: 'FAULT_CODE', width: 80},
            {label: '故障原因', name: 'faultReason', index: 'FAULT_REASON', width: 80},
            {
                label: '类型', name: 'type', index: 'TYPE', width: 80, formatter: function (value) {
                    return sysEnums.FAULT_TYPE[value];
                }
            },
            {label: '设备小类ID', name: 'devTypeIdS', index: 'DEV_TYPE_ID_S', width: 80, hidden: true},
            {label: '设备中类ID', name: 'devTypeIdM', index: 'DEV_TYPE_ID_M', width: 80, hidden: true},
            {label: '设备大类ID', name: 'devTypeIdB', index: 'DEV_TYPE_ID_B', width: 80, hidden: true},
        ]
    });
    vm.getDevTypeB();

});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "typePid",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },

        callback: {
            onClick: function () {
                var node = faultTree.getSelectedNodes();
                vm.q.id = node[0].id;
                vm.query()
            }
        }
    }
};
var ztree;
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        faultBase: {
            id: '',
            faultType: null,
        },
        // faultType: {
        //     parentName: null,
        //     typePid: '0'
        // },
        devTypeBList: {},
        devTypeMList: {},
        devTypeSList: {},
        ruleValidate: {

        },
        q: {
            faultCode:'',
            devTypeIdB:'',
            devTypeIdM:'',
            devTypeIdS:'',
            type:''
        },

        TypeArray: enumsToList("FAULT_TYPE")
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";

            vm.getFaultTypeTree();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getFaultTypeTree();
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.faultBase.id == (null || '') ? "../faultbase/save" : "../faultbase/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.faultBase),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../faultbase/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../faultbase/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.faultBase = r.faultBase;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                faultCode:'',
                devTypeIdB:'',
                devTypeIdM:'',
                devTypeIdS:'',
                type:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        //查询设备大类
        getDevTypeB: function () {
            Ajax.request({
                url: "../devtype/queryAll",
                params: {typeTp: 2},
                async: true,
                successCallback: function (r) {
                    vm.devTypeBList = r.list;
                }
            });
        },
        //查询设备中类
        getDevTypeM: function (id) {
            if (id) {
                Ajax.request({
                    url: "../devtype/queryAll",
                    params: {typePid: id,typeTp: 1},
                    async: true,
                    successCallback: function (r) {
                        vm.devTypeMList = r.list;
                    }
                });
            }
        },
        //查询设备小类
        getDevTypeS: function (id) {
            if (id) {
                Ajax.request({
                    url: "../devtype/queryAll",
                    params: {typePid: id,typeTp: 0},
                    async: true,
                    successCallback: function (r) {
                        vm.devTypeSList = r.list;
                    }
                });
            }
        },
        getFaultTypeTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../faulttype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.faultList;
                    ztree = $.fn.zTree.init($("#faultTree"), setting, tree);
                }
            });
        },
        faultTree: function () {
            openWindow({
                title: "选择故障类型",
                area: ['300px', '450px'],
                content: jQuery("#faultLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    // vm.faultBase.id = node[0].id;
                    vm.faultBase.faultType = node[0].typeName;
                    layer.close(index);
                }
            });
        },
    }
});
